﻿using System;
using System.Reflection;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Microsoft.SharePoint;

namespace CMSE.Utilities
{
    public struct FeatureHandler
    {
        /// <summary>
        /// Register EventReceiver to a content type
        /// </summary>
        /// <param name="targetWeb">web of the content type</param>
        /// <param name="cts">content type</param>
        /// <param name="type">Eventrecever Type</param>
        /// <param name="assembly">Assembly of the event receiver</param>
        /// <param name="classname">Classname of the event receiver</param>
        public void RegisterEventReceiver(SPWeb targetWeb, string contenttypename, SPEventReceiverType type, Assembly assembly, string classname)
        {
            using (SPSite site = new SPSite(targetWeb.Url))
            {
                using (SPWeb web = site.OpenWeb())
                {
                    SPContentType contentType = null;
                    try
                    {
                        contentType = web.ContentTypes[contenttypename];
                    }
                    catch { }

                    if (contentType != null)
                    {
                        contentType.EventReceivers.Add(type, assembly.FullName, assembly.GetName().Name + classname);
                        contentType.Update(true);
                    }
                }
            }

        }

        /// <summary>
        /// UnRegister EventReceiver to a content type
        /// </summary>
        /// <param name="targetWeb">web of the content type</param>
        /// <param name="cts">content type</param>
        /// <param name="type">Eventrecever Type</param>
        /// <param name="assembly">Assembly of the event receiver</param>
        /// <param name="classname">Classname of the event receiver</param>
        public void UnRegisterAllEventReceiver(SPWeb targetWeb, string contenttypename, Assembly assembly, string classname)
        {
            using (SPSite site = new SPSite(targetWeb.Url))
            {
                using (SPWeb web = site.OpenWeb())
                {
                    SPContentType contentType = null;
                    try
                    {
                        contentType = web.ContentTypes[contenttypename];
                    }
                    catch { }

                    if (contentType != null)
                    {
                        for (int i = 0; i < contentType.EventReceivers.Count; i++)
                        {
                            if (contentType.EventReceivers[i].Assembly == assembly.FullName)
                            {
                                contentType.EventReceivers[i].Delete();
                                contentType.Update(true);
                            }

                        }
                        contentType.Update(true);
                    }

                }
            }
        }
    }
}
